package com.xgry.bpm.api.base.testTable.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgry.bpm.api.base.testTable.entity.TestTable;
import com.xgry.bpm.api.base.testTable.service.TestTableService;
import com.xgry.bpm.api.base.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.xgry.bpm.common.utils.Result;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author adrian
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/test-table")
public class TestTableController {

    @Autowired
    private TestTableService testTableService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @GetMapping("/getList")
    public Result getList(){
        List<TestTable> list1 = testTableService.list();
        List<TestTable> list = testTableService.getTestTableList();
        //强制查询主库 @mater
        List<TestTable> list2 = testTableService.getTestTableList(1);
        //User user  = (User) SecurityUtils.getSubject().getPrincipal();
        return Result.success(HttpStatus.OK.getReasonPhrase(),list);
    }

    @GetMapping("/getTableList")
    public Result getTableList(){
        List<TestTable> testTableList = testTableService.getTestTableList();
        return Result.success(HttpStatus.OK.getReasonPhrase(),testTableList);
    }

    @PostMapping("/getTableForPage")
    public Result getTableForPage(@RequestParam("pageIndex")Integer pageIndex,@RequestParam("pageSize")Integer pageSize,TestTable testTable){
        IPage<TestTable> tableForPage = testTableService.getTableForPage(new Page<TestTable>(pageIndex, pageSize), testTable);

        System.out.println(tableForPage);

        return Result.convertIpageToSuccess(tableForPage);
    }

    @GetMapping("/insertTestTable")
    public Result insertTestTable(@RequestBody TestTable testTable){
        return Result.success(HttpStatus.OK.getReasonPhrase(),testTableService.insertTable(testTable));
    }

    @PostMapping("/signIn")
    public Result signIn(@RequestBody User user){
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        User queryUser = (User) subject.getPrincipal();
        return Result.success(HttpStatus.OK.getReasonPhrase(),queryUser);
    }


}
